<template>
  <div class="info_config_page">
    <div class="common_form_search">
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item label="品类名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入品类名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="供应商名称">
          <el-input
            v-model="searchForm.shopname"
            placeholder="请输入供应商名称"
            clearable
          />
        </el-form-item>
        <!-- <el-form-item label="审核状态" v-if="activeName == 0">
          <el-select v-model="searchForm.ischeck" placeholder="全部" clearable>
            <el-option label="已审核" :value="1"></el-option>
            <el-option label="待审核" :value="2"></el-option>
            <el-option label="拒绝审核" :value="3"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="申请时间">
          <my-date-picker ref="myDatePicker" />
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="search()"
          >查询</el-button>
          <el-button size="small" @click="reset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider />

    <el-tabs v-model="activeName" @tab-click="handleTabsClick">
      <el-tab-pane :label="`全部(${totalNum})`" name="0" />
      <el-tab-pane :label="`待审核品类(${auditNum})`" name="2" />
      <el-tab-pane :label="`已审核品类(${approvedNum})`" name="1" />
      <el-tab-pane :label="`已拒绝品类(${refusedNum})`" name="3" />
    </el-tabs>
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon
          style="margin-right: 15px"
          icon-class="list"
          :size="15"
        />数据列表
      </h5>
      <el-dropdown
        v-if="($hasMethod('#delete') || $hasMethod('#add')) && activeName != 0"
        trigger="click"
        @command="handleCommand"
      >
        <el-button size="small" class="add-btn svg-btn" plain>
          <svg-icon icon-class="batch" :size="11" />批量操作
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-if="$hasMethod('#add') && activeName == 2"
            command="add"
          >通过</el-dropdown-item>
          <el-dropdown-item
            v-if="$hasMethod('#refuse') && activeName == 2"
            command="refuse"
          >拒绝</el-dropdown-item>
          <el-dropdown-item
            v-if="$hasMethod('#delete')"
            command="delete"
          >删除</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column
        align="center"
        type="selection"
        width="55"
        :selectable="
          (row, index) => {
            return row.rules !== '*';
          }
        "
      />
      <el-table-column align="center" label="ID" prop="id" width="80" />
      <el-table-column align="center" label="品类名称" prop="category_name" />
      <el-table-column align="center" label="供应商名称">
        <template slot-scope="scope">
          <el-link
            :underline="false"
            type="primary"
            @click="jumpSupplier(scope.row.shopname)"
          >{{ scope.row.shopname }}</el-link>
          <!-- href="/supplier/list?shopname='scope.row.shopname' " -->
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="商家ID"
        prop="member_id"
        width="120"
      />
      <el-table-column label="审核状态" align="center" width="110">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.ischeck == 1
                ? 'success'
                : scope.row.ischeck == 2
                  ? ''
                  : 'danger'
            "
          >{{
            scope.row.ischeck == 1
              ? "已审核"
              : scope.row.ischeck == 2
                ? "待审核"
                : "拒绝审核"
          }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="申请时间"
        prop="create_time"
        width="100"
      />
      <el-table-column
        align="center"
        label="订单来源和费率"
        prop="category_rate"
      >
        <template slot-scope="scope">
          <el-link
            :underline="false"
            type="primary"
            @click="checkRate(scope.row)"
          >{{ scope.row.category_rate }}</el-link>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="260" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="$hasMethod('#add') && scope.row.ischeck == 2"
            size="mini"
            type="primary"
            @click="handleDialog('add', scope.row)"
          >通过</el-button>
          <el-button
            v-if="$hasMethod('#refuse') && scope.row.ischeck == 2"
            size="mini"
            type="warning"
            @click="handleDialog('refuse', scope.row.id)"
          >拒绝</el-button>
          <el-button
            v-if="$hasMethod('#edit') && scope.row.ischeck == 1"
            size="mini"
            type="primary"
            @click="handleDialog('edit', scope.row)"
          >设置费率</el-button>
          <el-button
            v-if="$hasMethod('#delete')"
            size="mini"
            type="danger"
            @click="handleDialog('del', scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <custom-page
      ref="customPage"
      :total="total"
      @getList="getList"
    />
    <el-dialog title="拒绝审核" :visible.sync="refuseVisible" width="500px">
      <el-form ref="refuseForm" :model="refuseForm" :rules="rules">
        <el-form-item
          label="原因："
          label-width="120px"
          prop="check_refuse_type"
        >
          <el-select
            v-model="refuseForm.check_refuse_type"
            placeholder="请选择"
            @change="changeOption"
          >
            <el-option
              v-for="item in refuseList"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="其他原因："
          label-width="120px"
          :rules="[
            { required: isRequired, message: '请输入原因', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="refuseForm.check_refuse_reason"
            type="textarea"
            placeholder="请输入备注原因信息！例如：图片不符合规范"
            prop="check_refuse_reason"
            :rows="3"
            maxlength="50"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="refuseVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('refuseForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
    <rate-dialog
      :show-visible="rateVisible"
      :rate-data="rateData"
      @closeDialog="closeDialog"
    />
    <set-dialog
      :set-rate-visible="setRateVisible"
      :select-val="selectVal"
      :source-list="sourceList"
      :source-form="formList"
      @closeDialog="closeDialog"
      @deleteFrom="deleteFrom"
      @addSource="addSource"
      @submitSource="submitSource"
      @changeSourceId="changeSourceId"
    />
    <batch-pass
      :is-show-refuse-from="isShowRefuseFrom"
      :is-show-rate-from="isShowRateFrom"
      :dialog-title="dialogTitle"
      :refuse-list="refuseList"
      :select-val="selectVal"
      :source-list="sourceList"
      :source-form="formList"
      :batch-pass-title="batchPassTitle"
      :batch-pass-total="batchPassTotal"
      :batch-pass-data="batchPassData"
      :batch-pass-visible="batchPassVisible"
      :batch-refuse-form="batchRefuseForm"
      @closeDialog="closeDialog"
      @submitPass="submitPass"
      @changeOption="changeOption"
      @submitSource="submitSource"
      @addSource="addSource"
      @deleteFrom="deleteFrom"
      @changeSourceId="changeSourceId"
    />
  </div>
</template>

<script>
import {
  categoryList,
  editUserIscheck,
  delUserInfo,
  getSourceInfo,
  getUserByIdStr,
  getSourceAndRate,
  setRate
} from '@/api/audit'
import customPage from '@/components/customPage'
import myDatePicker from '@/components/myDatePicker'
import rateDialog from '../category/rateDialog'
import setDialog from '../category/setDialog'
import batchPass from '../category/batchPass'
export default {
  components: { customPage, rateDialog, setDialog, batchPass, myDatePicker },
  data() {
    return {
      searchForm: {
        name: '',
        shopname: '',
        ischeck: null,
        create_time: ''
      },
      activeName: '0',
      total: 0,
      tableData: [],
      refuseVisible: false,
      refuseForm: {
        check_refuse_type: '',
        check_refuse_reason: ''
      },
      batchRefuseForm: {
        check_refuse_type: '',
        check_refuse_reason: ''
      },
      refuseList: [
        { value: 1, text: '营业执照经营范围不包含该品类' },
        { value: 2, text: '其他原因' }
      ],
      formList: [
        {
          source_id: 7,
          is_delete: 0,
          smartcontract: 0.00,
          realname: 0.00,
          risk: 0.00,
          platform: 8.00,
          withhold: 1.60,
          file: 0.45,
          financing: 0.00
        },
        {
          source_id: 8,
          is_delete: 0,
          smartcontract: 0.00,
          realname: 0.00,
          risk: 0.00,
          platform: 8.00,
          withhold: 1.60,
          file: 0.45,
          financing: 0.00
        }
      ],
      rules: {
        check_refuse_type: [
          { required: true, message: '请选择拒绝原因', trigger: 'change' }
        ]
      },
      refuseId: 0,
      isRequired: false,
      totalNum: 0,
      auditNum: 0,
      approvedNum: 0,
      refusedNum: 0,
      rateVisible: false,
      rateData: [],
      setRateVisible: false,
      selectVal: '',
      submitType: '',
      sourceList: [],
      batchPassVisible: false,
      batchPassData: [],
      dialogTitle: '',
      batchPassTitle: '',
      batchPassTotal: 0,
      isShowRefuseFrom: false,
      isShowRateFrom: false,
      setMemberId: '',
      setCategoryId: '',
      delList: []
    }
  },
  mounted() {
    this.getList()
    this.formList = []
  },
  methods: {
    // 查询
    search() {
      const pickerVal = this.$refs.myDatePicker.getVal()
      if (pickerVal !== 'error') {
        this.searchForm.create_time = pickerVal
        this.$refs.customPage.page = 1
        this.getList()
      }
    },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset()
      }
      this.searchForm = {
        name: '',
        shopname: '',
        ischeck: null,
        create_time: ''
      }
      this.getList()
    },
    handleTabsClick(tab) {
      this.activeName = tab.name
      this.getList(true)
    },
    jumpSupplier(val) {
      this.$router.push({ path: '/supplier/list', query: { shopname: val }})
    },
    checkRate(row) {
      this.rateVisible = true
      getSourceAndRate({
        member_id: row.member_id,
        category_id: row.category_id
      }).then((res) => {
        this.rateData = res
      })
    },
    closeDialog() {
      this.rateVisible = false
      this.setRateVisible = false
      this.batchPassVisible = false
    },
    submitPass(param) {
      // console.log('submitPass', this.isShowRateFrom, param)
      let ids = ''
      const idlist = []
      const goodsRates = []
      this.batchPassData.forEach((item) => {
        idlist.push(item.id)
        goodsRates.push({
          member_id: item.member_id,
          category_id: item.category_id
        })
      })
      ids = idlist.join(',')
      if (this.isShowRateFrom) {
        editUserIscheck({
          checkType: 'pass',
          ischeck: 1,
          idStr: ids,
          goods_rate: goodsRates,
          rate_list: param
        }).then((res) => {
          this.batchPassVisible = false
          this.getList(true)
        })
      } else {
        const { check_refuse_type, check_refuse_reason } = this.batchRefuseForm
        if (!check_refuse_type) {
          this.$message.warning('请选择原因')
          return false
        } else if (check_refuse_type == 2 && !check_refuse_reason) {
          this.$message.warning('请输入其他原因')
          return false
        }
        editUserIscheck({
          checkType: 'refuse',
          idStr: ids,
          ischeck: 3,
          check_refuse_type,
          check_refuse_reason
        }).then((res) => {
          this.batchPassVisible = false
          this.getList(true)
        })
      }
    },
    deleteFrom(item, index) {
      // this.index = this.sourceForm.moreFroms.indexOf(item)
      // this.formList.splice(index, 1)
      const delData = [...this.formList]
      if (delData.length > 0) {
        delData.forEach((x, index1) => {
          if (index1 == index) {
            x['is_delete'] = 1
            this.formList.splice(index, 1)
          } else {
            x['is_delete'] = 0
          }
        })
      }
      this.delList = delData
    },
    addSource() {
      this.formList.push({
        source_id: '',
        is_delete: 0,
        smartcontract: 0.00,
        realname: 0.00,
        risk: 0.00,
        platform: 8.00,
        withhold: 1.60,
        file: 0.45,
        financing: 0.00
      })
    },
    submitSource(list) {
      const rateList = this.delList.length > 0 ? this.delList : list
      const obj = {
        member_id: this.setMemberId,
        category_id: this.setCategoryId
      }
      if (this.submitType == 'set') {
        setRate({
          ...obj,
          rate_list: rateList
        }).then((res) => {
          this.setRateVisible = false
          this.getList()
        })
      } else {
        const params = [].concat(obj)
        // console.log('通过',this.submitType, list, rateList )
        editUserIscheck({
          checkType: 'pass',
          ischeck: 1,
          idStr: this.idString,
          goods_rate: params,
          rate_list: rateList
        }).then((res) => {
          this.setRateVisible = false
          this.getList()
        })
      }
    },
    // 批量操作
    handleCommand(command) {
      const list = this.$refs.multipleTable.selection
      this.batchPassTotal = list.length
      if (list.length === 0) {
        this.$message({
          message: '没有勾选项',
          type: 'warning'
        })
        return
      }
      this.setMemberId = list[0].member_id
      this.setCategoryId = list[0].category_id
      const ids = list.map((item) => item.id).join(',')
      // console.log('ids',ids)
      switch (command) {
        case 'add':
          this.handleDialog('add', ids, list)
          break
        case 'refuse':
          this.handleDialog('refuse', ids, list)
          break
        case 'delete':
          this.handleDialog('del', ids, list)
          break

        default:
          break
      }
    },
    handleDialog(type, id, list) {
      console.log('id', id)
      const ischeck = type == 'add' ? 1 : 3
      this.isShowRefuseFrom = false
      if (type == 'add') {
        if (!list) {
          this.idString = id.id
          this.submitType = 'pass'
          this.getRate(id)
        } else {
          this.isShowRateFrom = true
          this.dialogTitle = '批量通过审核'
          this.batchPassTitle = '是否确认下列商品批量通过审核？'
          this.batchPassVisible = true
          this.batchPassData = list
          this.formList = [
            {
              source_id: 7,
              is_delete: 0,
              smartcontract: 0.00,
              realname: 0.00,
              risk: 0.00,
              platform: 8.00,
              withhold: 1.60,
              file: 0.45,
              financing: 0.00
            },
            {
              source_id: 8,
              is_delete: 0,
              smartcontract: 0.00,
              realname: 0.00,
              risk: 0.00,
              platform: 8.00,
              withhold: 1.60,
              file: 0.45,
              financing: 0.00
            }
          ]
          getSourceInfo({
            member_id: this.setMemberId,
            category_id: this.setCategoryId
          }).then((res) => {
            this.sourceList = res
          })
        }
      } else if (type == 'del') {
        this.$confirm('您确定要删除吗?', '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          delUserInfo({
            idStr: id
          }).then((res) => {
            this.getList()
          })
        })
      } else if (type == 'refuse') {
        if (!list) {
          this.refuseForm = Object.assign({})
          this.refuseId = id
          this.isRequired = false
          this.refuseVisible = true
        } else {
          this.dialogTitle = '批量拒绝审核'
          this.batchPassTitle = '是否确认下列商品批量拒绝审核？'
          this.isShowRateFrom = false
          this.isShowRefuseFrom = true
          this.batchPassVisible = true
          this.batchPassData = list
        }
      } else if (type == 'edit') {
        this.submitType = 'set'
        this.getRate(id)
      }
    },
    getRate(params) {
      this.formList = [
        {
          source_id: 7,
          is_delete: 0,
          smartcontract: 0.00,
          realname: 0.00,
          risk: 0.00,
          platform: 8.00,
          withhold: 1.60,
          file: 0.45,
          financing: 0.00
        },
        {
          source_id: 8,
          is_delete: 0,
          smartcontract: 0.00,
          realname: 0.00,
          risk: 0.00,
          platform: 8.00,
          withhold: 1.60,
          file: 0.45,
          financing: 0.00
        }
      ]
      this.setRateVisible = true
      this.setMemberId = params.member_id
      this.setCategoryId = params.category_id
      this.delList = []
      const obj = {
        member_id: params.member_id,
        category_id: params.category_id
      }
      getSourceAndRate(obj).then((res) => {
        if (res.length > 0) {
          res.forEach((x) => {
            x['is_delete'] = 0
            this.formList = res
          })
        }
      })
      getSourceInfo(obj).then((res) => {
        this.sourceList = res
      })
    },
    changeSourceId(val) {
      // console.log("val",val)
    },
    changeOption(val) {
      this.$nextTick(() => {
        if (val == 2) {
          this.isRequired = true
        } else {
          this.isRequired = false
        }
      })
    },
    submitForm(formName) {
      const { check_refuse_type, check_refuse_reason } = this.refuseForm
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log(valid, check_refuse_type, check_refuse_reason);
          if (check_refuse_type == 2) {
            if (!check_refuse_reason) {
              this.$message.warning('请输入其他原因')
              return false
            }
          }
          editUserIscheck({
            checkType: 'refuse',
            idStr: this.refuseId,
            ischeck: 3,
            check_refuse_type,
            check_refuse_reason
          }).then((res) => {
            this.refuseVisible = false
            this.getList()
          })
        }
      })
    },
    // 获取列表
    getList(flag) {
      const { page, pageSize } = this.$refs.customPage
      if (!flag) {
        this.tableData = []
      }
      categoryList({
        page,
        pageSize,
        name: this.searchForm.name,
        shopname: this.searchForm.shopname,
        ischeck: this.searchForm.ischeck || this.activeName,
        create_time: this.searchForm.create_time
      }).then((res) => {
        this.tableData = res.data || []
        if (this.activeName == 0) {
          this.total = res.total
        } else if (this.activeName == 2) {
          this.total = res.count.notcheck_total || 0
        } else if (this.activeName == 1) {
          this.total = res.count.ischeck_total || 0
        } else {
          this.total = res.count.refusecheck_total || 0
        }
        this.totalNum = res.count.check_total || 0
        this.auditNum = res.count.notcheck_total || 0
        this.approvedNum = res.count.ischeck_total || 0
        this.refusedNum = res.count.refusecheck_total || 0
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
